package com.northming.daytodo.controller.graph;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.northming.daytodo.conts.SourceConts;
import com.northming.daytodo.conts.StatusConts;
import com.northming.daytodo.domain.graph.GraphInfo;
import com.northming.daytodo.domain.graph.GraphItem;
import com.northming.daytodo.domain.graph.GraphTodo;
import com.northming.daytodo.domain.graph.GraphTree;
import com.northming.daytodo.domain.graph.UserGraph;
import com.northming.daytodo.domain.todo.Todo;
import com.northming.daytodo.domain.todo.TodoStatus;
import com.northming.daytodo.mapper.graph.GraphMapper;
import com.northming.daytodo.mapper.todo.TodoMapper;
import com.northming.daytodo.util.TimeUtil;
import com.northming.daytodo.util.UserUtil;

@RestController
@RequestMapping("/graph")
public class GraphController {
	
	
	@Autowired
	private GraphMapper graphMapper;
	
	@Autowired
	private TodoMapper todoMapper;
	
	@Autowired
	private  UserUtil userUtil;
	
	
	/**
	 * get graph list
	 * @param type
	 * @return graph list
	 */
	@RequestMapping("/list")
	public Object getGraphList(String type){
		
		return graphMapper.getGraphList(type, StatusConts.USE);
	}
	/**
	 * get graph
	 * @param id
	 * @return graph info
	 */
	@RequestMapping("/{id}")
	public Object getGraph(@PathVariable("id") Integer id){
		//graphMapper.getGraph(id);
		//get graphinfo
		GraphInfo graphInfo = graphMapper.getGraphInfo(id);
		//get graph items
		List<GraphItem> graphItems = graphMapper.getGraphItems(id,StatusConts.USE);
		GraphItem indexGraphItem = new GraphItem();
		indexGraphItem.setId(0);
		indexGraphItem.setLevel(0);
		indexGraphItem.setName(graphInfo.getName());	
		graphItems.add(0, indexGraphItem);
		//get graph status
		boolean isGraphUsed = graphMapper.isGraphUsed(id,userUtil.getUserId(),StatusConts.USE);
		
		Map<String, Object> map  = new HashMap<>();
		map.put("isGraphUsed", isGraphUsed);
		map.put("graphInfo", graphInfo);
		map.put("graphItems", graphItems);
		return map;
	}
	
	@RequestMapping("/appToMyGraph")
	@Transactional
	public Object appToMyGraph(UserGraph userGraph){
		List<GraphTodo> lastGraphItems = graphMapper.getLastGraphItems(userGraph.getGraphId(),StatusConts.USE);			
		if(lastGraphItems.size()>0){
			for (int i = 0; i < lastGraphItems.size(); i++) {
				GraphTodo tempGraphTodo = lastGraphItems.get(i);
				tempGraphTodo.setStartDate(TimeUtil.getOtherDay((i+1)/4));
				
			}
		}
		return lastGraphItems;
	}
	@RequestMapping("/saveGraphTodo")
	@Transactional
	public Object saveGraphTodo(@RequestParam(defaultValue="")String ids,@RequestParam(defaultValue="")String dates,Integer graphId){	
		Integer userId = userUtil.getUserId();
		int successNum=0;
		//insert user graph
		UserGraph userGraph = new UserGraph();
		userGraph.setCreateTime(TimeUtil.getNow());
		userGraph.setCreateUser(userId);
		userGraph.setStatus(StatusConts.USE);
		userGraph.setUserId(userId);
		userGraph.setGraphId(graphId);
		successNum+=graphMapper.insertUserGraph(userGraph);
		if(!ids.equals("")){
			String[] idArr = ids.split(",");
			String[] dateArr = dates.split(",");
			for (int i = 0; i < idArr.length; i++) {
				Integer graphItemId = Integer.valueOf(idArr[i]);
				//get graph id detail
				GraphItem tempGraphItem = graphMapper.getGraphItem(graphItemId);
				Todo todo = new Todo();
				todo.setName(tempGraphItem.getName());
				todo.setDetail(tempGraphItem.getDetail());
				todo.setStartDate(dateArr[i]);
				todo.setEndDate(dateArr[i]);
				todo.setCreateUser(userId);
				todo.setCreateTime(TimeUtil.getNow());
				todo.setStatus(StatusConts.USE);
				TodoStatus todoStatus = new TodoStatus();
				todoStatus.setUserId(userId);
				todoStatus.setStatus(StatusConts.USE);
				todoStatus.setSourceId(SourceConts.GRAPH);
				todoStatus.setSourceRefId(graphItemId);
				//insert todo
				successNum+= todoMapper.insertTodo(todo);
				if(todo.getId()!=null){
					todoStatus.setTodoId(todo.getId());
					//insert todo_status
					successNum+= todoMapper.insertTodoStatus(todoStatus);
				}
			}
			return successNum==idArr.length*2+1 ? 1:0;
		}else{
			return successNum;
		}
		
	}
	
	
	@RequestMapping("/toMyGraph")
	public Object toMyGraph(Integer graphId){
		Map<String, Object> map  = new HashMap<>();
		int graphTodoSize = graphMapper.getGraphToDoSize(graphId,userUtil.getUserId(),SourceConts.GRAPH);
		if(graphTodoSize<0){
			map.put("graphItem", graphMapper.getLastGraphItems(graphId,StatusConts.USE));			
		}else{
			map.put("graphTodos",showMyGraph(graphId));
		}
		return map;
	}
	
	@RequestMapping("/showMyGraph")
	public Object showMyGraph(Integer graphId){
		//get graphinfo
		GraphInfo graphInfo = graphMapper.getGraphInfo(graphId);
		//get graph items
		List<GraphTodo> graphTodos = graphMapper.getGraphTodos(graphId,userUtil.getUserId(),SourceConts.GRAPH);
		GraphTodo indexGraphTodo = new GraphTodo();
		indexGraphTodo.setId(0);
		indexGraphTodo.setLevel(0);
		indexGraphTodo.setName(graphInfo.getName());	
		graphTodos.add(0, indexGraphTodo);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("graphInfo", graphInfo);
		map.put("graphItems", graphTodos);
		return map;
	}
	
	
	@RequestMapping("/myGraphStore")
	public Object myGraphStore(){
		Integer userId = userUtil.getUserId();
		return graphMapper.getMyGraphList(userId, StatusConts.USE);
	}

	
	
	
	
}
